package com.easemob.chat;

import android.content.Context;
import com.easemob.analytics.EMPerformanceCollector;
import com.easemob.analytics.EMTimeTag;
import com.easemob.chat.core.XmppConnectionManager;
import com.easemob.chat.core.e;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.EMLog;
import com.easemob.util.NetUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jivesoftware.smack.PrivacyList;
import org.jivesoftware.smack.PrivacyListManager;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterStorage;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smack.packet.PrivacyItem;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;

public class EMContactManager
{
  private static final String TAG = "contact";
  Map<String, EMContact> contactTable = new Hashtable(100);
  private EMRosterListener rosterListener = null;
  private Roster roster;
  RosterStorageImpl rosterStorage;
  private static EMContactManager instance = null;
  private static final String black_list_name = "special";
  boolean enableRosterVersion = true;
  EMContactListener contactListener = null;
  Set<String> deleteContactsSet = null;
  List<String> blackList = null;
  private boolean hasGetBlacklist = false;
  private static final String BROADCAST_CONTACT_CHANGED_ACTION = "com.easemob.contact.changed";
  private XmppConnectionManager xmppConnectionManager;
  private Context context;
  private boolean isIntied = false;

  public static EMContactManager getInstance()
  {
    if (instance == null)
      instance = new EMContactManager();
    return instance;
  }

  void init(Context paramContext, XmppConnectionManager paramXmppConnectionManager)
  {
    if (this.isIntied)
      return;
    EMLog.d("contact", "try to init contact manager");
    this.context = paramContext;
    this.xmppConnectionManager = paramXmppConnectionManager;
    this.deleteContactsSet = Collections.synchronizedSet(new HashSet());
    if (this.enableRosterVersion)
      loadContacts();
    Object localObject;
    if (needGetRosterFromServer())
    {
      localObject = new EMTimeTag();
      ((EMTimeTag)localObject).start();
      this.roster = paramXmppConnectionManager.getConnection().getRoster();
      EMPerformanceCollector.collectRetrieveRoster(this.roster.getEntries().size(), ((EMTimeTag)localObject).stop());
    }
    else
    {
      this.roster = paramXmppConnectionManager.getConnection().getRosterWithoutLoad();
    }
    if ((!this.hasGetBlacklist) && (EMChatConfig.getInstance().isGetBlackList))
      try
      {
        localObject = getBlackListUsernamesFromServer();
        if (localObject != null)
          saveBlackList((List)localObject);
        this.hasGetBlacklist = true;
      }
      catch (EaseMobException localEaseMobException)
      {
      }
    this.rosterListener = new EMRosterListener(this, this.roster);
    this.roster.addRosterListener(this.rosterListener);
    this.isIntied = true;
    EMLog.d("contact", "created contact manager");
  }

  boolean needGetRosterFromServer()
  {
    return (e.a().j()) || (EMChatManager.getInstance().getChatOptions().getUseRoster());
  }

  public void addContact(String paramString1, String paramString2)
    throws EaseMobException
  {
    addContactToRosterThroughPresence(paramString1.toLowerCase(), paramString2);
  }

  public void deleteContact(String paramString)
    throws EaseMobException
  {
    removeContactFromRoster(paramString);
    EMChatManager.getInstance().deleteConversation(paramString);
  }

  void addContactInternal(EMContact paramEMContact)
  {
    EMLog.d("contact", "internal add contact:" + paramEMContact.eid);
    this.contactTable.put(paramEMContact.username, paramEMContact);
    e.a().b(paramEMContact.eid, paramEMContact.username);
  }

  void deleteContactInternal(String paramString)
  {
    EMLog.d("contact", "delete contact:" + paramString);
    EMContact localEMContact = (EMContact)this.contactTable.remove(paramString);
    if (localEMContact != null)
      e.a().m(localEMContact.eid);
    else
      EMLog.w("contact", "local contact doesnt exists will try to delete:" + paramString);
    localEMContact = null;
    EMChatManager.getInstance().deleteConversation(paramString, false);
  }

  EMContact getContactByUserName(String paramString)
  {
    EMContact localEMContact = (EMContact)this.contactTable.get(paramString);
    if (localEMContact == null)
      localEMContact = new EMContact(paramString);
    return localEMContact;
  }

  void removeContactFromRoster(String paramString)
    throws EaseMobException
  {
    try
    {
      RosterEntry localRosterEntry = this.roster.getEntry(getEidFromUserName(paramString));
      if (localRosterEntry != null)
        this.roster.removeEntry(localRosterEntry);
    }
    catch (Exception localException)
    {
      EMLog.e("contact", "Failed to delete contact:", localException);
      throw new EaseMobException("Failed to delete contact:" + localException);
    }
  }

  void removeContactByUsername(String paramString)
  {
    EMContact localEMContact = (EMContact)this.contactTable.remove(paramString);
    if (localEMContact != null)
      e.a().m(localEMContact.eid);
    EMChatManager.getInstance().deleteConversation(paramString, false);
    EMLog.d("contact", "removed contact:" + localEMContact);
    localEMContact = null;
  }

  public void reset()
  {
    this.contactTable.clear();
    this.blackList.clear();
    this.hasGetBlacklist = false;
    this.roster = null;
    this.rosterStorage = null;
    removeContactListener();
    this.isIntied = false;
  }

  RosterStorage getRosterStorage(Context paramContext)
  {
    if (this.rosterStorage == null)
      this.rosterStorage = new RosterStorageImpl(paramContext, this);
    return this.rosterStorage;
  }

  List<String> getRosterUserNames()
    throws EaseMobException
  {
    EMChatManager.getInstance().checkConnection();
    if (this.enableRosterVersion)
      loadContacts();
    EMLog.d("contact", "start to get roster for user:" + EMSessionManager.getInstance().getLoginUserName());
    this.roster = this.xmppConnectionManager.getConnection().getRoster();
    Collection localCollection = this.roster.getEntries();
    EMLog.d("contact", "get roster return size:" + localCollection.size());
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = localCollection.iterator();
    while (localIterator.hasNext())
    {
      RosterEntry localRosterEntry = (RosterEntry)localIterator.next();
      EMLog.d("contact", "entry name:" + localRosterEntry.getName() + " user:" + localRosterEntry.getUser());
      if ((localRosterEntry.getType() == RosterPacket.ItemType.both) || (localRosterEntry.getType() == RosterPacket.ItemType.from))
      {
        String str = localRosterEntry.getName();
        if ((str == null) || (str.equals("")))
          str = getUserNameFromEid(localRosterEntry.getUser());
        if (str.startsWith(EMChatConfig.getInstance().APPKEY))
          str = str.substring((EMChatConfig.getInstance().APPKEY + "_").length());
        EMLog.d("contact", "get roster contact:" + str);
        localArrayList.add(str);
      }
    }
    return localArrayList;
  }

  void addContactToRosterThroughPresence(String paramString1, String paramString2)
    throws EaseMobException
  {
    try
    {
      EMChatManager.getInstance().checkConnection();
      Presence localPresence = new Presence(Presence.Type.subscribe);
      String str = getEidFromUserName(paramString1);
      localPresence.setTo(str);
      if ((paramString2 != null) && (!"".equals(paramString2)))
        localPresence.setStatus(paramString2);
      EMSessionManager.getInstance().getConnection().sendPacket(localPresence);
    }
    catch (Exception localException)
    {
      throw new EaseMobException(localException.getMessage());
    }
  }

  static String getBareEidFromUserName(String paramString)
  {
    return EMChatConfig.getInstance().APPKEY + "_" + paramString;
  }

  static String getEidFromUserName(String paramString)
  {
    if (paramString.contains("@"))
      return paramString;
    if (paramString.equals("bot"))
      return "bot@echo.easemob.com";
    EMChatConfig.getInstance();
    return EMChatConfig.getInstance().APPKEY + "_" + paramString + "@" + EMChatConfig.DOMAIN;
  }

  public static String getUserNameFromEid(String paramString)
  {
    String str1;
    if (paramString.contains("@"))
      str1 = paramString.substring(0, paramString.indexOf("@"));
    else
      str1 = paramString;
    if ((str1 == null) || ("".equals(str1)))
      str1 = paramString;
    String str2 = str1;
    if (str2.startsWith(EMChatConfig.getInstance().APPKEY))
      return str2.substring((EMChatConfig.getInstance().APPKEY + "_").length());
    return str2;
  }

  static String getEidFromGroupId(String paramString)
  {
    if (paramString.contains("@"))
      return paramString;
    return EMChatConfig.getInstance().APPKEY + "_" + paramString + EMChatConfig.MUC_DOMAIN_SUFFIX;
  }

  static String getGroupIdFromEid(String paramString)
  {
    String str1;
    if (paramString.contains("@"))
      str1 = paramString.substring(0, paramString.indexOf("@"));
    else
      str1 = paramString;
    if ((str1 == null) || ("".equals(str1)))
      str1 = paramString;
    String str2 = str1;
    if (str2.startsWith(EMChatConfig.getInstance().APPKEY))
      return str2.substring((EMChatConfig.getInstance().APPKEY + "_").length());
    return str2;
  }

  String getCurrentUserFullJid()
  {
    String str1 = EMSessionManager.getInstance().currentUser.username;
    String str2 = XmppConnectionManager.getXmppResource(this.context);
    String str3 = getEidFromUserName(str1);
    String str4 = str3 + "/" + str2;
    return str4;
  }

  public List<String> getContactUserNames()
    throws EaseMobException
  {
    return getRosterUserNames();
  }

  public void setContactListener(EMContactListener paramEMContactListener)
  {
    this.contactListener = paramEMContactListener;
  }

  public void removeContactListener()
  {
    this.contactListener = null;
  }

  public void addUserToBlackList(String paramString, boolean paramBoolean)
    throws EaseMobException
  {
    String str = getEidFromUserName(paramString);
    addToPrivacyList(str, paramBoolean);
    e.a().o(paramString);
    if (!this.blackList.contains(paramString))
      this.blackList.add(paramString);
  }

  public void deleteUserFromBlackList(String paramString)
    throws EaseMobException
  {
    String str = getEidFromUserName(paramString);
    deleteFromPrivacyList(str);
    e.a().n(paramString);
    if (this.blackList.contains(paramString))
      this.blackList.remove(paramString);
  }

  public List<String> getBlackListUsernames()
  {
    if (this.blackList.size() == 0)
    {
      List localList = e.a().i();
      if (localList.size() != 0)
        this.blackList.addAll(localList);
    }
    return this.blackList;
  }

  public List<String> getBlackListUsernamesFromServer()
    throws EaseMobException
  {
    ArrayList localArrayList = new ArrayList();
    if ((this.xmppConnectionManager.getConnection() == null) || (!this.xmppConnectionManager.getConnection().isConnected()))
      throw new EaseMobException("connetion is not connected");
    PrivacyListManager localPrivacyListManager = PrivacyListManager.getInstanceFor(this.xmppConnectionManager.getConnection());
    if (localPrivacyListManager != null)
      try
      {
        PrivacyList localPrivacyList = localPrivacyListManager.getPrivacyList("special");
        if (localPrivacyList != null)
        {
          List localList = localPrivacyList.getItems();
          Iterator localIterator = localList.iterator();
          while (localIterator.hasNext())
          {
            PrivacyItem localPrivacyItem = (PrivacyItem)localIterator.next();
            String str = localPrivacyItem.getValue();
            localArrayList.add(getUserNameFromEid(str));
          }
        }
      }
      catch (XMPPException localXMPPException)
      {
        if ((localXMPPException == null) || (localXMPPException.getMessage() == null) || (!localXMPPException.getMessage().contains("item-not-found")))
          throw new EaseMobException(localXMPPException.getMessage());
      }
    return localArrayList;
  }

  public void saveBlackList(List<String> paramList)
  {
    if (paramList != null)
    {
      this.blackList.clear();
      this.blackList.addAll(paramList);
      e.a().b(paramList);
    }
  }

  private void addToPrivacyList(String paramString, boolean paramBoolean)
    throws EaseMobException
  {
    try
    {
      PrivacyListManager localPrivacyListManager = PrivacyListManager.getInstanceFor(this.xmppConnectionManager.getConnection());
      if (localPrivacyListManager == null)
        throw new EaseMobException("PrivacyListManager is null");
      PrivacyList[] arrayOfPrivacyList = localPrivacyListManager.getPrivacyLists();
      Object localObject2;
      if (arrayOfPrivacyList.length == 0)
      {
        localObject1 = new ArrayList();
        localObject2 = new PrivacyItem("jid", false, 100);
        if (!paramBoolean)
          ((PrivacyItem)localObject2).setFilterMessage(true);
        ((PrivacyItem)localObject2).setValue(paramString);
        ((List)localObject1).add(localObject2);
        localPrivacyListManager.updatePrivacyList("special", (List)localObject1);
        localPrivacyListManager.setDefaultListName("special");
        localPrivacyListManager.setActiveListName("special");
        return;
      }
      Object localObject1 = localPrivacyListManager.getPrivacyList("special");
      if (localObject1 != null)
      {
        localObject2 = ((PrivacyList)localObject1).getItems();
        int i = 0;
        Iterator localIterator = ((List)localObject2).iterator();
        while (localIterator.hasNext())
        {
          localPrivacyItem = (PrivacyItem)localIterator.next();
          String str = localPrivacyItem.getValue();
          EMLog.d("contact", "addToPrivacyList item.getValue=" + localPrivacyItem.getValue());
          if (str.equalsIgnoreCase(paramString))
          {
            i = 1;
            break;
          }
        }
        if (i != 0)
        {
          EMLog.d("contact", "current user is already in black list");
          return;
        }
        PrivacyItem localPrivacyItem = new PrivacyItem("jid", false, 100);
        localPrivacyItem.setValue(paramString);
        ((List)localObject2).add(localPrivacyItem);
        EMLog.d("contact", "addToPrivacyList item.getValue=" + localPrivacyItem.toXML());
        localPrivacyListManager.updatePrivacyList("special", (List)localObject2);
        localPrivacyListManager.setActiveListName("special");
      }
    }
    catch (Exception localException)
    {
      throw new EaseMobException(localException.getMessage());
    }
  }

  private void deleteFromPrivacyList(String paramString)
    throws EaseMobException
  {
    if ((this.xmppConnectionManager == null) || (this.xmppConnectionManager.getConnection() == null))
      throw new EaseMobException("connection is null, please login first");
    PrivacyListManager localPrivacyListManager = PrivacyListManager.getInstanceFor(this.xmppConnectionManager.getConnection());
    if (localPrivacyListManager == null)
      throw new EaseMobException("PrivacyListManager is null");
    try
    {
      PrivacyList[] arrayOfPrivacyList = localPrivacyListManager.getPrivacyLists();
      if (arrayOfPrivacyList.length != 0)
      {
        PrivacyList localPrivacyList = localPrivacyListManager.getPrivacyList("special");
        if (localPrivacyList != null)
        {
          List localList = localPrivacyList.getItems();
          if ((localList != null) && (localList.size() != 0))
          {
            int i = 0;
            Iterator localIterator = localList.iterator();
            while (localIterator.hasNext())
            {
              PrivacyItem localPrivacyItem = (PrivacyItem)localIterator.next();
              String str = localPrivacyItem.getValue();
              EMLog.d("contact", "PrivacyList item.getValue=" + localPrivacyItem.getValue());
              if (str.equalsIgnoreCase(paramString))
              {
                localList.remove(localPrivacyItem);
                i = 1;
                break;
              }
            }
            if (i == 0)
            {
              EMLog.d("contact", "current user is not exsit in the black list");
              return;
            }
            localPrivacyListManager.declineDefaultList();
            localPrivacyListManager.updatePrivacyList("special", localList);
            if (localList.size() > 0)
            {
              localPrivacyListManager.setDefaultListName("special");
              localPrivacyListManager.setActiveListName("special");
            }
          }
          else
          {
            EMLog.d("contact", "current user is not exsit in the black list");
          }
        }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      throw new EaseMobException(localException.getMessage());
    }
  }

  public static String getContactChangeAction()
  {
    return "com.easemob.contact.changed_" + EMChatConfig.getInstance().APPKEY;
  }

  void checkConnection()
    throws EaseMobException
  {
    if (this.xmppConnectionManager == null)
      return;
    if (this.xmppConnectionManager.getConnection() == null)
      return;
    if ((!this.xmppConnectionManager.getConnection().isConnected()) || (!this.xmppConnectionManager.getConnection().isAuthenticated()))
    {
      EMLog.e("contact", "network unconnected");
      if (NetUtils.hasDataConnection(EMChatConfig.getInstance().applicationContext))
        EMLog.d("contact", "try to reconnect after check connection failed");
    }
  }

  void loadContacts()
  {
    if ((!EMChatManager.getInstance().getChatOptions().getUseRoster()) && (!this.enableRosterVersion))
    {
      EMLog.d("contact", "roster is disabled, skip load contacts from db");
      return;
    }
    if (e.a() == null)
    {
      EMLog.d("contact", "first time exec. no contact db");
      return;
    }
    List localList = e.a().h();
    Iterator localIterator = localList.iterator();
    while (localIterator.hasNext())
    {
      EMContact localEMContact = (EMContact)localIterator.next();
      this.contactTable.put(localEMContact.username, localEMContact);
    }
    EMLog.d("contact", "loaded contacts:" + this.contactTable.size());
    if (this.rosterStorage != null)
    {
      EMLog.d("contact", "sync roster storage with db");
      this.rosterStorage.loadEntries();
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMContactManager
 * JD-Core Version:    0.6.1
 */